/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "ssi_debugger.h"
#include "ssi_engine.h"
#include "ssi_interpreter.h"
#include "ssi_debugger.h"

CDebugger::CDebugger(void)
{
	debug_LastLine = 0;
	debug_GoCtr = 0;
	debug_ToLine = 0;
	input = 0;
	output = 0;
}

CDebugger::~CDebugger(void)
{
}

SSI_INT64 CDebugger::Process(CEngine* Engine, CInterpreter* Interp, CInput* Input, const CScriptStatement& aStatement){
	return 1;
}

SSI_INT64 CDebugger::AddBreakpoint(CBreakpoint& newBreakpoint)
{
	BreakpointNode* aNode = Breakpoints.find(newBreakpoint);
	if(aNode){
		return 1;
	}
	aNode = Breakpoints.push(newBreakpoint);
	if(aNode){
		return 1;
	}
	return 0;
}

SSI_INT64 CDebugger::RemoveBreakpoint(CBreakpoint& inBreakpoint)
{
	BreakpointNode* aNode = Breakpoints.find(inBreakpoint);
	if(!aNode){
		return 1;
	}

	return Breakpoints.clear_range(aNode, aNode);
}

SSI_INT64 CDebugger::CheckBreakpoint(CBreakpoint& inBreakpoint)
{
	BreakpointNode* aNode = Breakpoints.find(inBreakpoint);
	if(aNode){
		return 1;
	}
	return 0;
}



/*
SSI_INT64 CScriptClient::ProcessDebug(){
	
	String Line;
	String Tok;
	String Delim = " ";
	String Rem;
	String sMess;
	SSI_INT64 Pos;

	int bLoop = 1;
	socket_stream* a_stream = (socket_stream*)input;

	while(bLoop){
		if(!a_stream->getDebug()){
			// Do we just wait?
			return 0;
		}
		if(a_stream->m_Cmd.ID == SSI_VAR_RQST){
			CVariable aVar;
			String sVar;
			aVar.Name = a_stream->m_Cmd.Data;
			aVar.SetType(dUNKNOWN);
			if(!Engine->GetVariable(aVar)){
				sVar.Format("Variable '%s' not found\n", aVar.Name.Str());
			}else{
				sVar = aVar.GetWorkingData().GetString().Str();
			}
			a_stream->m_Cmd.Set(SSI_VAR_RSP, sVar.GetLength(), sVar.Str());
			if(!a_stream->sendDebug()){
				bLoop = 0;
			}
		}else if(a_stream->m_Cmd.ID == SSI_DBG_STEP){
			// A go single line
			bLoop = 0;
		}else{
			bLoop = 0;
		}
	}
	return 1;
}
*/
SSI_INT64 CDebugger::GetDebugInput(String& FileName, CScriptLine& CurrLine, String& Input)
{
	return 0;
}

SSI_INT64 CDebugger::SetGoCounter(SSI_INT64 NewCtr)
{
	debug_GoCtr = NewCtr;
	return 1;
}

SSI_INT64 CDebugger::SetToLine(SSI_INT64 NewTo)
{
	debug_ToLine = NewTo;
	return 1;
}

SSI_INT64 CDebugger::DisplayDebugError(String& sError)
{
	String sErr = " ** DEBUG Error: ";
	sErr = sErr + sError;
	output->Disp(sErr.Str(), 1);
	return 1;
}

SSI_INT64 CDebugger::DisplayVariable(String& VarName, CEngine* InEngine)
{
  String sMess;
	CVariable aVar;
	aVar.Name = VarName;
	aVar.SetType(dUNKNOWN);
	if(!InEngine->GetVariable(aVar)){
		sMess.Format("Variable '%s' not found", aVar.Name.Str());
    return output->DisplayDebugVariable(sMess);
	}else{
		sMess.Format("%s", aVar.GetWorkingData().GetString().Str());
		return output->DisplayDebugVariable(sMess);
	}
}




